﻿@using SelfMadeMillionaire.Core.Entities
@model YahooFinanceWatcher.Models.HomeModel
@{
    ViewBag.Title = "Yahoo Finance Watcher";
    Layout = "~/Views/Shared/_MainLayout.cshtml";
}

<div class="row admin_wrapper">
	<div class="col-sm-12 col-md-12 col-lg-12" style="margin-top:10px;">

        <div class="div_group">
            @Html.ActionLink("SymbolOptionsHistories", "Index",  "SymbolOptionsHistory")
        </div>
        @if (Model.Portfolios.Any())
        {
        <div class="div_group" style = "margin-top:10px;">
		<div class="page-header" style="margin-top:10px;">
            <b>Available Portfolio</b>
        </div>
        <div>
            @foreach (Portfolio item in Model.Portfolios)
            {
                @Html.ActionLink(item.PortfolioName, "Index", new { ID = item.ID})
                <span style="margin-left: 10px;"></span>
            }
        </div>
        </div>
        }

        <div class="div_group" style = "margin-top:10px;">

        <div class="portfolio_name">
            <h3 style="display:inline; margin-right:10px;">@Model.SelectedName</h3>
            @if (Model.SelectedId > 0)
            {
                @Html.ActionLink("New", "Index", new { ID = (int?)null }, new { @class = "btn btn-default" })
                <button type="button" id="btnEditPortfolio" class="btn btn-default edit-portfolio" data-id="@Model.SelectedId">Edit</button>
                using (Html.BeginForm("DeletePortfolio", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "formDelete", style = "display:inline;" }))
                {
                    @Html.Hidden("id", Model.SelectedId)
                    <button type="submit" id="btnDeletePortfolio" class="btn btn-default" onclick="return confirm('Do you want delete Portfolio?');">Delete</button>
                }
            }
            else
            {
                <button type="button" id="btnSaveNewPortfolio" class="btn btn-default edit-portfolio" data-id="@Model.SelectedId">Save</button>
            }
        </div>

		@using (Html.BeginForm("GetData", "Home", FormMethod.Post, new { @class = "form-horizontal", id = "formFilter", style = "margin-top:20px;" }))
        {
				@Html.ValidationSummary()
                @Html.HiddenFor(x => x.SelectedId)

                <div class="row">
                    <div class="col-sm-3">
				        <div class="form-group">
					        @Html.LabelFor(model => model.NameCompany, new { @class = "col-sm-6 control-label" })
					        <div class="col-sm-6">
						        @Html.TextBoxFor(m => m.NameCompany, new { @class = "form-control", placeholder = "Enter Symbol" })
                                @Html.ValidationMessageFor(model => model.NameCompany)
					        </div>
				        </div>
			        </div>
                    <div class="col-sm-2">
        		        <button type="submit" id="btnFindCompany" class="btn btn-default">Add Symbol</button>
			        </div>
		        </div>
        }

		<div class="table_container">
		    <div id="divInfoList">
			    @Html.Partial("CompanyInfoList", Model.InfoList)
			</div>
		</div>

        </div>


    </div>
</div>

<div id="edit-modal-container" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="edit-dialog-label" aria-hidden="true">
</div>

<script type="text/javascript">
    
    function RefreshSymbols() {
        var infoUrl = '@Url.Action("GetSymbols", new { id = Model.SelectedId })';
        $('#divInfoList').load(infoUrl, function () {
        });
    }

    $(document).ready(function () {
        @if (Model.NotFoundSymbol)
        {
            <text>alert('Symbol not found');</text>
        }

        setInterval(function () { RefreshSymbols() }, 60000);
    });

    $(".edit-portfolio").click(function () {

        $.ajax({
            url: '@Url.Action("PortfolioDialog", "Home")',
                data: { id: $(this).attr('data-id') },
			    cache: false,
			    success: function (result) {
			        $("#edit-modal-container").html(result);
			        $("#edit-modal-container").modal();
			    }
			});

        return false;
    });

</script>

